//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbBangCap
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbBangCap_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbBangCap_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbBangCap OTbBangCap)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenBangCap", OTbBangCap.TenBangCap);
            pr[1] = new SqlParameter(@"TrangThai", OTbBangCap.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbBangCap_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbBangCap OTbBangCap)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaBangCap", OTbBangCap.MaBangCap);
            pr[1] = new SqlParameter(@"TenBangCap", OTbBangCap.TenBangCap);
            pr[2] = new SqlParameter(@"TrangThai", OTbBangCap.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbBangCap_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaBangCap)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaBangCap",MaBangCap);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbBangCap_Delete", pr);
        }
//-----------------------------------------------------------//

        public static bool KiemTraDuLieu(ETbBangCap BangCap)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenBangCap", BangCap.TenBangCap);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbBangCap_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static DataTable SearchBangCap(ETbBangCap BangCap)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenBangCap", BangCap.TenBangCap);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbBangCap_Search", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static ETbBangCap SelectByID(int MaBangCap)
        {
            ETbBangCap OTbBangCap = new ETbBangCap();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaBangCap",MaBangCap);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbBangCap_SelectByID", pr);
            if (idr.Read())
              OTbBangCap = GetOneTbBangCap(idr);
            idr.Close();
            idr.Dispose();
            return OTbBangCap;
        }
//-----------------------------------------------------------//
        private static ETbBangCap GetOneTbBangCap(IDataReader idr)
       {
            ETbBangCap OTbBangCap = new ETbBangCap();
                if (idr["MaBangCap"] != DBNull.Value)
                    OTbBangCap.MaBangCap = (int)idr["MaBangCap"];
                if (idr["TenBangCap"] != DBNull.Value)
                    OTbBangCap.TenBangCap = (string)idr["TenBangCap"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbBangCap.TrangThai = (bool)idr["TrangThai"];
            return OTbBangCap;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaBangCap)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaBangCap",MaBangCap);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbBangCap_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbBangCap> ListAll()
        {
            List<ETbBangCap> list = new List<ETbBangCap>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbBangCap_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbBangCap(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbBangCap> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbBangCap> list = new List<ETbBangCap>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbBangCap_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbBangCap(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}